const compilerSFC = require('@vue/compiler-sfc');
const qs = require('querystring');
const minify = require('html-minifier').minify;

module.exports = function (resoures) {
  const query = qs.parse(this.query.slice(1))
  const { type, index } = query;
  const sfc = compilerSFC.parse(resoures);

  if (type === 'script') {
    const script = sfc.descriptor.script ? sfc.descriptor.script.content : '';
    return script;
  } else if (type === 'template') {
    var template = sfc.descriptor.template.content;
    return minify(template, { removeComments: true, collapseWhitespace: true, minifyJS: true, minifyCSS: true });
  } else if (type === 'style') {
    var style = sfc.descriptor.styles[index];
    return minify(style.content, { removeComments: true, collapseWhitespace: true, minifyJS: true, minifyCSS: true });
  }
}